﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using NextCMS.Models;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class Templates : ITemplate
    {
        private string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/Templates.xml";
        XDocument doc = null;
        public Templates()
        {
            doc = XDocument.Load(FilePath);
        }
        public TemplateModel GetById(int id)
        {
            var q = from d in doc.Descendants("Template")
                    where (int)d.Element("Id") == id
                    select new TemplateModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        Name = d.Element("Name").Value,
                        WebResourceId = (int)d.Element("WebResourceId"),
                        Content = d.Element("Content").Value,
                        TemplateImageName = d.Element("TemplateImageName").Value,
                        lstPageSections = (from s in d.Elements("Sections").Elements("Section")
                                           select new PageSections
                                           {
                                               Id = new Guid(s.Element("Id").Value),
                                               Name = s.Element("Name").Value,
                                               SectionId = (int)s.Element("SectionId")
                                           }).ToList()
                    };
            return q.FirstOrDefault();
        }
        public IEnumerable<TemplateModel> GetAllBySiteId(int SiteId)
        {
            var q = from d in doc.Descendants("Template")
                    where (int)d.Element("SiteId") == SiteId
                    select new TemplateModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        Name = d.Element("Name").Value,
                        WebResourceId = (int)d.Element("WebResourceId"),
                        Content = d.Element("Content").Value,
                        TemplateImageName = d.Element("TemplateImageName").Value
                    };
            return q;
        }
        public int Save(TemplateModel page)
        {
            var q = from d in doc.Descendants("Template")
                    select new TemplateModel
                    {
                        Id = (int)d.Element("Id"),
                    };

            if (q.Count() > 0)
                page.Id = q.Max(s => s.Id) + 1;
            else
                page.Id = 1;

            XElement sections = new XElement("Sections", null);
            foreach (PageSections ps in page.lstPageSections)
            {
                XElement section = new XElement("Section",
                    new XElement("Id", ps.Id),
                    
                    new XElement("Name", ps.Name),
                    new XElement("SectionId", ps.SectionId)
                    );
                sections.Add(section);
            }

            var oMember = new XElement("Template",
             new XElement("Id", page.Id),
             new XElement("SiteId", page.SiteId),
             new XElement("Name", page.Name),
             new XElement("WebResourceId", page.WebResourceId),
             new XElement("Content", page.Content),
             new XElement("TemplateImageName", page.TemplateImageName),
             sections
             );

            doc.Root.Add(oMember);
            doc.Save(FilePath);
            return page.Id;
        }
        // Delete Record
        public void Delete(int id)
        {
            doc.Root.Elements("Template").Where(i => (int)i.Element("Id") == id).Remove();
            doc.Save(FilePath);
        }

        // Edit Record
        public void Edit(TemplateModel page)
        {
            XElement node = doc.Root.Elements("Template").Where(i => (int)i.Element("Id") == page.Id).FirstOrDefault();


            node.Element("Sections").RemoveAll();

            foreach (PageSections ps in page.lstPageSections)
            {
                XElement section = new XElement("Section",
                    new XElement("Id", ps.Id),
                    new XElement("Name", ps.Name),
                    new XElement("SectionId", ps.SectionId)
                    );
                node.Element("Sections").Add(section);
            }
            node.SetElementValue("SiteId", page.SiteId);
            node.SetElementValue("Content", page.Content);
            node.SetElementValue("Name", page.Name);
            node.SetElementValue("WebResourceId", page.WebResourceId);
            node.SetElementValue("TemplateImageName", page.TemplateImageName);
            doc.Save(FilePath);
        }

        public void Dispose()
        {
            doc = null;
        }
    }
}
